
//
//  NSDate+TTExtension.m
//  FlowerTown
//
//  Created by Chris on 2017/9/11.
//  Copyright © 2017年 TT. All rights reserved.
//

#import "NSDate+TTExtension.h"
#import "NSDate+JKExtension.h"

@implementation NSDate (TTExtension)

+ (NSString *)tt_timeInfoWithDateString:(NSString *)dateString {
    NSDate *date = [self jk_dateWithString:dateString format:[self jk_ymdHmsFormat]];
    
    NSDate *curDate = [NSDate date];
    NSTimeInterval time = -[date timeIntervalSinceDate:curDate];
    
    int month = (int)([curDate jk_month] - [date jk_month]);
    int year = (int)([curDate jk_year] - [date jk_year]);
    int day = (int)([curDate jk_day] - [date jk_day]);
    
    NSTimeInterval retTime = 1.0;
    if (time < 60) { // 小于1分钟
        return @"1分钟前";
    }else if (time < 3600) { // 小于一小时
        retTime = time / 60;
        retTime = retTime <= 0.0 ? 1.0 : retTime;
        return [NSString stringWithFormat:@"%.0f分钟前", retTime];
    }else if (time < 3600 * 24) { // 小于一天，也就是今天
//        retTime = time / 3600;
        div_t hour_div = div(time, 3600);
        div_t minute_div = div(hour_div.rem, 60);
        retTime = hour_div.quot <= 0.0 ? 1.0 : hour_div.quot;
//        retTime = retTime <= 0.0 ? 1.0 : retTime;
        
        return [NSString stringWithFormat:@"%.0f小时%@前", retTime, minute_div.quot <= 0.0 ? @"" : [NSString stringWithFormat:@"%.0d分钟", minute_div.quot]];
    } else if (time < 3600 * 24 * 2) {
        return @"昨天";
    }
    // 第一个条件是同年，且相隔时间在一个月内
    // 第二个条件是隔年，对于隔年，只能是去年12月与今年1月这种情况
    else if ((abs(year) == 0 && abs(month) <= 1)
             || (abs(year) == 1 && [curDate jk_month] == 1 && [date jk_month] == 12)) {
        int retDay = 0;
        if (year == 0) { // 同年
            if (month == 0) { // 同月
                retDay = day;
            }
        }
        
        if (retDay <= 0) {
            // 获取发布日期中，该月有多少天
            int totalDays = (int)[self jk_daysInMonth:date month:[date jk_month]];
            
            // 当前天数 + （发布日期月中的总天数-发布日期月中发布日，即等于距离今天的天数）
            retDay = (int)[curDate jk_day] + (totalDays - (int)[date jk_day]);
        }
        
        return [NSString stringWithFormat:@"%d天前", (abs)(retDay)];
    } else  {
        if (abs(year) <= 1) {
            if (year == 0) { // 同年
                return [NSString stringWithFormat:@"%d月前", abs(month)];
            }
            
            // 隔年
            int month = (int)[curDate jk_month];
            int preMonth = (int)[date jk_month];
            //这个第三方错误
            if (/*month == 12 && preMonth == 12*/ month == preMonth) {// 隔年，但同月，就作为满一年来计算
                return @"1年前";
            }
            return [NSString stringWithFormat:@"%d个月前", (abs)(12 - preMonth + month)];
        }
        
        return [NSString stringWithFormat:@"%d年前", abs(year)];
    }
    return @"1小时前";
}
+ (BOOL)isCurrentDay:(NSDate *)aDate
{
    if (aDate==nil) return NO;
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:aDate];
    NSDate *otherDate = [cal dateFromComponents:components];
    if([today isEqualToDate:otherDate])
        return YES;
    
    return NO;
}

@end
